package com.example.myapplication.history;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.myapplication.R;

import java.util.List;

public class HistoryAdapter extends ArrayAdapter<HistoryData> {

    public HistoryAdapter(Context context, List<HistoryData> dataList) {
        super(context, 0, dataList);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.history_list_item, parent, false);
        }

        HistoryData currentItem = getItem(position);

        TextView startTimeTextView = listItemView.findViewById(R.id.startTimeValueTextView);
        TextView endTimeTextView = listItemView.findViewById(R.id.endTimeValueTextView);
        TextView amountTextView = listItemView.findViewById(R.id.amountValueTextView);
        TextView intervalTextView = listItemView.findViewById(R.id.intervalValueTextView);

        // 设置文本
        if (currentItem != null) {
            startTimeTextView.setText(currentItem.getStartTime());
            endTimeTextView.setText(currentItem.getEndTime());
            amountTextView.setText(String.valueOf(currentItem.getAmount()));
            intervalTextView.setText(String.valueOf(currentItem.getInterval()));
        }

        return listItemView;
    }
}